import unittest
from unittest.mock import MagicMock

import bs4

from buondua.scrappers.picture import PictureParser


class TestPictureParser(unittest.TestCase):
    def setUp(self):
        def get_document():
            with open('tests/fixtures/album.html', 'rb') as f:
                return bs4.BeautifulSoup(f.read(), 'html.parser')

        self.picture_parser = PictureParser()
        self.picture_parser.get_document = MagicMock(return_value=get_document())
        self.picture_parser.document = self.picture_parser.get_document()

    def test_extraction_data(self):
        self.assertEqual(
            self.picture_parser.extract_title(),
            b'Kang In-kyung (\xea\xb0\x95\xec\x9d\xb8\xea\xb2\xbd): IKOF-4 2023 Christmas Digital Photobook + PPV Bunny (98 photos ) '
        )
        self.assertEqual(
            self.picture_parser.extract_url(),
            b'https://buondua.com/kang-in-kyung-\xea\xb0\x95\xec\x9d\xb8\xea\xb2\xbd-ikof-4-2023-christmas-digital-photobook-ppv-bunny-98-photos-35006'
        )
        self.assertEqual(
            self.picture_parser.extract_pages(),
            [
                b'https://buondua.com/kang-in-kyung-\xea\xb0\x95\xec\x9d\xb8\xea\xb2\xbd-ikof-4-2023-christmas-digital-photobook-ppv-bunny-98-photos-35006',
                b'https://buondua.com/kang-in-kyung-\xea\xb0\x95\xec\x9d\xb8\xea\xb2\xbd-ikof-4-2023-christmas-digital-photobook-ppv-bunny-98-photos-35006?page=2',
                b'https://buondua.com/kang-in-kyung-\xea\xb0\x95\xec\x9d\xb8\xea\xb2\xbd-ikof-4-2023-christmas-digital-photobook-ppv-bunny-98-photos-35006?page=3',
                b'https://buondua.com/kang-in-kyung-\xea\xb0\x95\xec\x9d\xb8\xea\xb2\xbd-ikof-4-2023-christmas-digital-photobook-ppv-bunny-98-photos-35006?page=4',
                b'https://buondua.com/kang-in-kyung-\xea\xb0\x95\xec\x9d\xb8\xea\xb2\xbd-ikof-4-2023-christmas-digital-photobook-ppv-bunny-98-photos-35006?page=5'
            ]
        )
        self.assertEqual(
            self.picture_parser.extract_images(),
            [
                b'https://lux.mrcong.com/images/2024/01/18/Kang-In-kyung-IKOF-4-2023-Christmas-Digital-Photobook-PPV-Bunny-MissKON.com-001.webp',
                b'https://lux.mrcong.com/images/2024/01/18/Kang-In-kyung-IKOF-4-2023-Christmas-Digital-Photobook-PPV-Bunny-MissKON.com-002.webp',
                b'https://lux.mrcong.com/images/2024/01/18/Kang-In-kyung-IKOF-4-2023-Christmas-Digital-Photobook-PPV-Bunny-MissKON.com-003.webp',
                b'https://lux.mrcong.com/images/2024/01/18/Kang-In-kyung-IKOF-4-2023-Christmas-Digital-Photobook-PPV-Bunny-MissKON.com-004.webp',
                b'https://lux.mrcong.com/images/2024/01/18/Kang-In-kyung-IKOF-4-2023-Christmas-Digital-Photobook-PPV-Bunny-MissKON.com-005.webp',
                b'https://lux.mrcong.com/images/2024/01/18/Kang-In-kyung-IKOF-4-2023-Christmas-Digital-Photobook-PPV-Bunny-MissKON.com-006.webp',
                b'https://lux.mrcong.com/images/2024/01/18/Kang-In-kyung-IKOF-4-2023-Christmas-Digital-Photobook-PPV-Bunny-MissKON.com-007.webp',
                b'https://lux.mrcong.com/images/2024/01/18/Kang-In-kyung-IKOF-4-2023-Christmas-Digital-Photobook-PPV-Bunny-MissKON.com-008.webp',
                b'https://lux.mrcong.com/images/2024/01/18/Kang-In-kyung-IKOF-4-2023-Christmas-Digital-Photobook-PPV-Bunny-MissKON.com-009.webp',
                b'https://lux.mrcong.com/images/2024/01/18/Kang-In-kyung-IKOF-4-2023-Christmas-Digital-Photobook-PPV-Bunny-MissKON.com-010.webp',
                b'https://lux.mrcong.com/images/2024/01/18/Kang-In-kyung-IKOF-4-2023-Christmas-Digital-Photobook-PPV-Bunny-MissKON.com-011.webp',
                b'https://lux.mrcong.com/images/2024/01/18/Kang-In-kyung-IKOF-4-2023-Christmas-Digital-Photobook-PPV-Bunny-MissKON.com-012.webp',
                b'https://lux.mrcong.com/images/2024/01/18/Kang-In-kyung-IKOF-4-2023-Christmas-Digital-Photobook-PPV-Bunny-MissKON.com-013.webp',
                b'https://lux.mrcong.com/images/2024/01/18/Kang-In-kyung-IKOF-4-2023-Christmas-Digital-Photobook-PPV-Bunny-MissKON.com-014.webp',
                b'https://lux.mrcong.com/images/2024/01/18/Kang-In-kyung-IKOF-4-2023-Christmas-Digital-Photobook-PPV-Bunny-MissKON.com-015.webp',
                b'https://lux.mrcong.com/images/2024/01/18/Kang-In-kyung-IKOF-4-2023-Christmas-Digital-Photobook-PPV-Bunny-MissKON.com-016.webp',
                b'https://lux.mrcong.com/images/2024/01/18/Kang-In-kyung-IKOF-4-2023-Christmas-Digital-Photobook-PPV-Bunny-MissKON.com-017.webp',
                b'https://lux.mrcong.com/images/2024/01/18/Kang-In-kyung-IKOF-4-2023-Christmas-Digital-Photobook-PPV-Bunny-MissKON.com-018.webp',
                b'https://lux.mrcong.com/images/2024/01/18/Kang-In-kyung-IKOF-4-2023-Christmas-Digital-Photobook-PPV-Bunny-MissKON.com-019.webp',
                b'https://lux.mrcong.com/images/2024/01/18/Kang-In-kyung-IKOF-4-2023-Christmas-Digital-Photobook-PPV-Bunny-MissKON.com-020.webp'
            ]
        )

        self.assertEqual(
            self.picture_parser.extract_tags(),
            [
                b'Kang In-kyung',
                b'Sera'
            ]
        )

        self.assertEqual(
            self.picture_parser.generate_title_slug(b'Kang In-kyung (\xea\xb0\x95\xec\x9d\xb8\xea\xb2\xbd): IKOF-4 2023 Christmas Digital Photobook + PPV Bunny (98 photos ) '),
            b'kang-in-kyung-gangingyeong-ikof-4-2023-christmas-digital-photobook-ppv-bunny-98-photos'
        )

    def test_extract(self):
        data = self.picture_parser.extract()

        self.assertTrue('album' in data)

        self.assertTrue('pictures' in data)

        self.assertEqual(
            data['album'],
            {
                'album_url': b'https://buondua.com/kang-in-kyung-\xea\xb0\x95\xec\x9d\xb8\xea\xb2\xbd-ikof-4-2023-christmas-digital-photobook-ppv-bunny-98-photos-35006',
                'title': b'Kang In-kyung (\xea\xb0\x95\xec\x9d\xb8\xea\xb2\xbd): IKOF-4 2023 Christmas Digital Photobook + PPV Bunny (98 photos ) ',
                'title_slug': b'kang-in-kyung-gangingyeong-ikof-4-2023-christmas-digital-photobook-ppv-bunny-98-photos',
                'tags': [
                    b'Kang In-kyung',
                    b'Sera'
                ]
            }
        )

        # 5 pages (1 page defacto) 20 pic * 5 = 100
        self.assertEqual(
            len(data['pictures']),
            100
        )

        self.assertEqual(
            data['pictures'][:5],
            [
                {
                    'url': b'https://lux.mrcong.com/images/2024/01/18/Kang-In-kyung-IKOF-4-2023-Christmas-Digital-Photobook-PPV-Bunny-MissKON.com-001.webp',
                    'pic_num': 1
                },
                {
                    'url': b'https://lux.mrcong.com/images/2024/01/18/Kang-In-kyung-IKOF-4-2023-Christmas-Digital-Photobook-PPV-Bunny-MissKON.com-002.webp',
                    'pic_num': 2
                },
                {
                    'url': b'https://lux.mrcong.com/images/2024/01/18/Kang-In-kyung-IKOF-4-2023-Christmas-Digital-Photobook-PPV-Bunny-MissKON.com-003.webp',
                    'pic_num': 3
                },
                {
                    'url': b'https://lux.mrcong.com/images/2024/01/18/Kang-In-kyung-IKOF-4-2023-Christmas-Digital-Photobook-PPV-Bunny-MissKON.com-004.webp',
                    'pic_num': 4
                },
                {
                    'url': b'https://lux.mrcong.com/images/2024/01/18/Kang-In-kyung-IKOF-4-2023-Christmas-Digital-Photobook-PPV-Bunny-MissKON.com-005.webp',
                    'pic_num': 5
                },
            ]
        )

    def test_saving(self):
        from buondua.models import db_proxy, orm, MODELS, Album, Picture
        db = orm.SqliteDatabase(':memory:')
        db_proxy.initialize(db)
        db.create_tables(MODELS)

        with db:

            data = self.picture_parser.extract()

            self.picture_parser.save(data)

            albums = Album.select()
            self.assertEqual(
                albums.count(),
                1
            )

            self.assertEqual(
                albums[0].id,
                1
            )

            self.assertEqual(
                albums[0].album_url.encode('utf-8'),
                b'https://buondua.com/kang-in-kyung-\xea\xb0\x95\xec\x9d\xb8\xea\xb2\xbd-ikof-4-2023-christmas-digital-photobook-ppv-bunny-98-photos-35006'
            )

            pics = Picture.select().where(Picture.album_id == albums)

            self.assertEqual(
                pics.count(),
                100
            )

            self.assertEqual(
                pics[0].url.encode('utf-8'),
                b'https://lux.mrcong.com/images/2024/01/18/Kang-In-kyung-IKOF-4-2023-Christmas-Digital-Photobook-PPV-Bunny-MissKON.com-001.webp'
            )
        db.drop_tables(MODELS)
